package com.winit.openapi.tools.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.v2.WinitProductRecommendTestService;
import com.winit.oms.spi.v2.tools.command.ProductRecommendGenerateEGDDateTestCommand;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * winit接口:测试接口(提供给自动化)
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * bing.gu 	1.0  		2017年6月26日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/winit/tools/test")
public class WinitToolsProductTestController extends BaseController {

    @Resource
    private WinitProductRecommendTestService winitProductRecommendTestService;

    /**
     * 模拟生成EGD时间
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/generateEGDDate")
    public String recommend(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        ProductRecommendGenerateEGDDateTestCommand command = new ProductRecommendGenerateEGDDateTestCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 组装command
            command.setSLA(json.getIntValue("SLA"));
            command.setStandardTimeZone(null != json.get("startTimeZone") ? null : json.getString("startTimeZone"));
            command.setStartDate(json.getDate("startDate"));
            command.setStartTimeZone(null != json.get("startTimeZone") ? null : json.getString("startTimeZone"));
            command.setWarehouseCode(json.getString("warehouseCode"));
            command.setWinitProductCode(json.getString("winitProductCode"));
            command.setDestCountryCode(json.getString("destCountryCode"));
            command.setDestPostCode(json.getString("destPostCode"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 调用接口
        Date result = winitProductRecommendTestService.generateEGDDate(command);
        if (null != result) {
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("EGDDate", result);
            responseMsg.setData(resultMap);
        }
        return SUCCESS;
    }

}
